﻿using Dapper;
using DapperExtensions;
using Hangjing.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hangjing.SQLServerDAL
{
    public class UserShareIncomeLogDal : BaseDal<user_share_income_log>
    {
        /// <summary>
        ///  判断此用户是否已经完成实名，且给上线加过Limit了
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public bool chekcUserRecommended(int userid)
        {
            int logCount = 0;
            using (SqlConnection cn = new SqlConnection(DBConfig.MasterDBKey))
            {
                cn.Open();
                logCount = cn.Query("select 1 from trade_limit_log where from_user_id=@userid and source =2 ", new { userid = userid }).Count();
                cn.Close();
            }

            return logCount > 0 ? true : false;

        }

        public PagerResult<user_share_income_log> GetList(int? UserId = null,
            PagerParameterInfo pagerParameter = null, 
            List<SortParameterInfo> sortParameter = null)
        {
            int allcount = 0;
            List<user_share_income_log> list = new List<user_share_income_log>();
            using (SqlConnection cn = new SqlConnection(DBConfig.UserCenterDBKey))
            {
                cn.Open();
                //TODO
                List<IPredicate> predList = new List<IPredicate>();
                if (UserId.HasValue)
                {
                    predList.Add(Predicates.Field<user_share_income_log>(f => f.user_id, Operator.Eq, UserId.Value));
                }

                //排序规则
                List<ISort> sortList = this.ConvertSortList(sortParameter, Predicates.Sort<user_share_income_log>(s => s.Id, false));
                //条件组合
                PredicateGroup predGroup = new PredicateGroup() { Operator = GroupOperator.And, Predicates = predList };

                list = cn.GetPage<user_share_income_log>(predGroup, sortList, pagerParameter.PageIndex, pagerParameter.PageSize).ToList();
                allcount = cn.Count<user_share_income_log>(predGroup);

                cn.Close();
            }

            return new PagerResult<user_share_income_log>(allcount, list);
        }
    }
}
